package com.gm.commonlib.views.popupwindow;


import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.gm.commonlib.R;
import com.gm.commonlib.interfaces.ICallBackListener;
import com.gm.commonlib.interfaces.IResultListener;


/**
 * 运单支付运费弹窗
 *
 * @author ：hck
 * @date：2017/6/19
 */

public class Popupwindow {
    public static PopupWindow mPopupWindow;


    public synchronized  static PopupWindow showPopView(Activity activity, final View mainView) {
        if (mPopupWindow != null) {
            hiddenPop();
        }
        mPopupWindow = getPopupWindow(activity);
        mPopupWindow.setContentView(mainView);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
        return mPopupWindow;

    }

    public static PopupWindow showPopViewRight(Activity activity, final View mainView) {
        if (mPopupWindow != null) {
            hiddenPop();
        }
        mPopupWindow = getPopupWindow(activity);
        mPopupWindow.setContentView(mainView);
        mPopupWindow.setOutsideTouchable(true);
       // mPopupWindow.showAsDropDown(activity.getWindow().getDecorView(),0,0,Gravity.RIGHT);
        mPopupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.RIGHT, 0, 0);
        return mPopupWindow;

    }

    public synchronized static PopupWindow showPopCenterView(Activity activity, final View mainView) {
        if (mPopupWindow != null) {
            hiddenPop();
        }
        mPopupWindow = getPopupWindow(activity);
        mPopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupWindow.setContentView(mainView);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        return mPopupWindow;

    }

    public synchronized static PopupWindow showWrapCenterView(Activity activity, final View mainView) {
        if (mPopupWindow != null) {
            hiddenPop();
        }
        mPopupWindow = getPopupWindow(activity);
        mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setContentView(mainView);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        return mPopupWindow;

    }

    public static PopupWindow getPopupWindow(final Activity activity) {
        mPopupWindow = new PopupWindow();
        mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setTouchable(true);
      //  mPopupWindow.setAnimationStyle(R.style.showPopupAnimation);
        WindowManager.LayoutParams params = activity.getWindow().getAttributes();
        params.alpha = 0.7f;
        activity.getWindow().setAttributes(params);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
              resetWindowAlpha(activity);
            }
        });

        return mPopupWindow;
    }


    public synchronized static void hiddenPop() {
        try {
            if (mPopupWindow != null) {
                mPopupWindow.dismiss();
                mPopupWindow = null;
            }
        } catch (Exception e) {

        }

    }

    public synchronized static void hiddenPop(IResultListener listener) {
        try {
            if (mPopupWindow != null) {
                mPopupWindow.dismiss();
                mPopupWindow = null;
            }
        } catch (Exception e) {

        }
        listener.onResult(true);

    }

    public static void hiddenPop(Activity activity) {
        resetWindowAlpha(activity);
        hiddenPop();
    }

    private  static void resetWindowAlpha(Activity activity) {
        if (activity!=null && !activity.isFinishing()){
            WindowManager.LayoutParams params = activity.getWindow().getAttributes();
            params.alpha = 1f;
            activity.getWindow().setAttributes(params);
        }

    }
}
